$(function () {
    var gridSetting={
    		url: '../runtime/tasks',
    		 colModel: [			
    				{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
    				{ label: 'name', name: 'name', index: 'name', width: 80 }, 			
    				{ label: '认领人', name: 'assignee', index: 'assignee', width: 80 ,sortable:false }, 			
    				{ label: '代办人', name: 'owner', index: 'owner', width: 80 ,sortable:false }, 			
    				{ label: 'processDefinitionId', name: 'processDefinitionId', index: 'processDefinitionId', width: 80 ,sortable:false }, 			
    				{ label: 'processInstanceId', name: 'processInstanceId', index: 'processInstanceId', width: 80 ,sortable:false }, 			
    				{ label: 'taskDefinitionKey', name: 'taskDefinitionKey', index: 'taskDefinitionKey', width: 80,sortable:false  }, 			
    				{ label: '状态', name: 'suspended', index: 'suspended', width: 80,formatter:function(cellvalue, options, rowObject){
    					if (cellvalue) {
    						return "挂起";
    					}else{
    						return "活动";
    					}
    				},sortable:false   }, 			
    				{ label: 'businessKey', name: 'businessKey', index: 'businessKey', width: 80,sortable:false  }, 			
    				{ label: 'description', name: 'description', index: 'description', width: 80,sortable:false  }, 			
    				{ label: 'createTime', name: 'createTime', index: 'createTime', width: 80,sortable:false  }, 			
    				{ label: 'dueDate', name: 'dueDate', index: 'dueDate', width: 80 ,sortable:false }, 			
    	        ],
	}
    gridSetting=$.extend(true,baseJqGridSetting,gridSetting);
    $("#jqGrid").jqGrid(gridSetting);
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bpmTask: {},
		bpmProcessInstance: {},
	},
	methods: {
		query: function () {
			vm.reload();
		},
		showModel: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var selectedRowData=getSelectedRowData()
			parent.layer.open({
				type:2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "查看流程状态",
				area: ['1200px', '600px'],
				shade: 0,
				shadeClose: false,
				content: ['diagram-viewer/index.html?processDefinitionId='+selectedRowData.processDefinitionId+'&processInstanceId='+selectedRowData.processInstanceId, 'no'],
			});
		},
		//要求对某项任务负责
		claim: function (event) {//签收
			var param={};
			param.action="claim";
			param.assignee="1";//设置签收人
			//param.variables={};//这个传递参数，待分析
			this.commomPost(param);
		},
		complete: function (event) {//完成工作项
			var param={};
			param.action="complete";
			//param.variables={};//这个传递参数，待分析
			this.commomPost(param);
		},
		//将任务委托给另一个用户
		delegate: function (event) {//委派工作项
			var param={};
			param.action="delegate";
			//param.variables={};//这个传递参数，待分析
			this.commomPost(param);
		},
		//标志着受让人完成了这项任务，并可以将它发回给所有者。
		resolve: function (event) {//转发代办（别人办完还会回到自己这里）
			var param={};
			param.action="resolve";
			//param.variables={};//这个传递参数，待分析
			this.commomPost(param);
		},
		commomPost: function (param) {//完成工作项
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			$.ajax({
				type: "POST",
			    url: "../runtime/tasks/"+id,
			    dataType:"text",
			    data: JSON.stringify(param),
			    contentType: "application/json",
			    success: function(){
			    	vm.reload();
				}
			});
		},
		showTaskInfo:function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var param={};
			$.ajax({
				type: "GET",
			    url: "../runtime/tasks/"+id,
			    contentType: "application/json",
			    success: function(r){
			    	console.info(r)
				}
			});
		},
		showProcessInstanceInfo:function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var taskData=getSelectedRowData();
			var param={};
			$.ajax({
				type: "GET",
			    url: "../runtime/process-instances/"+taskData.processInstanceId,
			    contentType: "application/json",
			    success: function(r){
			    	console.info(r)
				}
			});
		},
		completeStrengthen:function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var param={};
			$.ajax({
				type: "GET",
				url: "../runtime/tasks/"+id,
				contentType: "application/json",
				success: function(r){
					vm.bpmTask=r;
					vm.openCompletedaDialog();
				}
			});
		},
		openCompletedaDialog:function(){
			if(vm.bpmTask){
				var processDefinitionId=vm.bpmTask.processDefinitionId;
				var taskId=vm.bpmTask.id;
				var nodeId=vm.bpmTask.taskDefinitionKey;
				var processInstanceId=vm.bpmTask.processInstanceId;
				$.ajax({
					type: "GET",
					url: "../bpm/flow/getRuntimeTaskButton/"+processDefinitionId+"/"+nodeId+"/",
					contentType: "application/json",
					success: function(r){
						var setting={
								title:"表单",
								content:"请做出你的选择",
						}
						var btn=[];
						var btnfunction=[];
						var data=r.data;
						for (let i = 0; i < data.length; i++) {
							btn.push(data[i].ruleName);
							setting['btn'+(i+1)]=function(index, layero){
							   var ruleId = data[i].id
							   vm.completeStrengthenDo(ruleId);
							   parent.layer.close(index)
							  }
						}
						setting.btn=btn;
						bpmUtils.getTaskVariables(taskId,"formPath",function(variable){
							
							
							bpmUtils.getProcessInstanceInfo(processInstanceId,function(processInstance){
								var url=""+variable.value+"/toFormInfo/"+processDefinitionId+"/"+nodeId+"/"+processInstance.businessKey+"/view"
								setting.type= 2;
								setting.area=[ '500px','500px'];
								setting.content=url
								parent.layer.open(setting);
								
							})
						})
						
					}
				});
			}
		},
		completeStrengthenDo:function(ruleId){
			
			$.ajax({
				type: "GET",
				url:"../bpm/flow/getRuntimeTaskButtonRule/"+ruleId,
				contentType: "application/json",
				success: function(r){
					console.info(r);
					var param={};
					var data=r.data;
					param.action="complete";
					var variables=[];
					for (let i = 0; i < data.length; i++) {
						var variable={};
						variable.name=data[i].bpmEnvName;
						variable.value=data[i].bpmEnvValue;
						variables.push(variable);
					}
					param['variables']=variables;
					vm.commomPost(param)
				}
			});
		},
		del: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "DELETE",
				    url: "../runtime/tasks/"+id,
				    contentType: "application/json",
				    success: function(r){
				    	alert('操作成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});